﻿<html>
<head>
<script>
  // The Page Action ID.
  var pageActionId = "RssPageAction";

  // The icon to use. This corresponds to the icon listed in the manifest.
  var subscribeId = 0;
  var subscribedId = 1;

  // A dictionary keyed off of tabId that keeps track of data per tab (for
  // example what feedUrl was detected in the tab).
  var feedData = {};
  
  localStorage["lastLoginCheckTime"] = (new Date()).getTime();
  
  if (localStorage["black_list"] == undefined) {
	var blacklist = new Array("http://www.google.com/reader/*", "https://www.google.com/reader/*");
	localStorage["black_list"] = JSON.stringify(blacklist);
  }
  
  function getSubList() {
	var xhr = new XMLHttpRequest();
	xhr.open("GET", 'https://www.google.com/reader/api/0/token', true);
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4) {
			if (xhr.status == 200) {
				var xhr2 = new XMLHttpRequest();
				xhr2.open("GET", 'http://www.google.com/reader/api/0/subscription/list', true);
				xhr2.onreadystatechange = function() {
					if (xhr2.readyState == 4 && xhr2.status == 200) {
						localStorage["lastSubLists"] = xhr2.responseText;
					}
				}
				xhr2.send();
			}
		}
	}
	xhr.send();
  }
  
  if (localStorage["check_option_interval"] > 0) {
   	getSubList();
  }

  chrome.extension.onConnect.addListener(function(port) {
	// determine if the site need to be check
	var blacklist = JSON.parse(localStorage["black_list"]);
	for (var i = 0; i < blacklist.length; i++) {
		var re = new RegExp(blacklist[i]);
		if (re.test(port.tab.url)) {
			return;
		}
	}
  
    if (localStorage["check_option_interval"] > 0) {
		// Detemine if user is logged in
		var curTime = (new Date()).getTime();
		if (curTime - localStorage["lastLoginCheckTime"] > localStorage["check_option_interval"] * 1000) {
			localStorage["lastLoginCheckTime"] = curTime;
			getSubList();
		}
	}
	
	// This will get called from the content script using PostMessage.
    // |feedUrls| is a list of URL feeds found on the page. We only need 1 to
    // enable the PageAction icon in the Omnibox.
    port.onMessage.addListener(function(feedUrls) {
      feedUrl = feedUrls[0];
	  
      // Let Chrome know that the PageAction needs to be enabled for this tabId
      // and for the url of this page.
      if (feedUrl) {
		var idx = 0;
		if (localStorage["check_option_interval"] > 0) {
			if (localStorage["lastSubLists"] != undefined && localStorage["lastSubLists"].length > 0) {
				idx = localStorage["lastSubLists"].indexOf(feedUrl);
			}
		}
		
        feedData[port.tab.id] = {pageUrl: port.tab.url,
                                 feedUrl: feedUrl};

        chrome.pageActions.enableForTab(
            pageActionId, {tabId: port.tab.id,
                           url: port.tab.url,
                           title: (idx > 0) ? "You have subscribed...." : "Click to subscribe....",
                           iconId: (idx > 0) ? subscribedId : subscribeId});
      }
    });
  });

  // Chrome will call into us when the user clicks on the icon in the OmniBox.
  chrome.pageActions["RssPageAction"].addListener(function(pageActionId,
      pageActionInfo) {
    chrome.windows.getCurrent(function(window) {
      chrome.tabs.get(pageActionInfo.tabId, function(tab) {
        // We need to know if we are the active window, because the tab may
        // have moved to another window and we don't want to execute this
        // action multiple times.
        if (window.focused) {
          // Create a new tab showing the subscription page with the right
          // feed URL.
          var url = localStorage["current_protocol"] + "://www.google.com/reader/view/feed/" +
              encodeURIComponent(feedData[pageActionInfo.tabId].feedUrl);
		  if (localStorage["tab_option"] == "new") {
			chrome.tabs.create({url: url, windowId: window.windowId});
		  } else {
			chrome.tabs.update(pageActionInfo.tabId, {url: url});
		  }
        }
      });
    });
  });

  chrome.tabs.onRemoved.addListener(function(reply) {
    feedData[reply.tabId] = null;
  });
  if ((localStorage["firstrun_grss"]!="false") && (localStorage["firstrun_grss"]!=false)){
  chrome.tabs.create({url: "options.html", selected:true})
  localStorage["firstrun_grss"] = false;
  localStorage["ver"]      = "1.0";
}
</script>
</head>
</html>
